package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.base.MyClientApplication;
import com.hzdesk.media.koreabatchmatvector.bean.Memory;
import com.hzdesk.media.koreabatchmatvector.bean.MouseUserDevicesinfoBean;
import com.hzdesk.media.koreabatchmatvector.bean.VideoFile;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.dao.MemoryDaoUtils;
import com.hzdesk.media.koreabatchmatvector.dao.MouseUserDevicesinfoDaoUtils;
import com.hzdesk.media.koreabatchmatvector.dao.VideoFileDaoUtils;
import com.hzdesk.media.koreabatchmatvector.logic.contract.BaseContract;
import com.hzdesk.media.koreabatchmatvector.utils.FileUtil;
import com.hzdesk.media.koreabatchmatvector.utils.MyToastUtils;
import com.hzdesk.media.koreabatchmatvector.utils.SDCardUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/6/22.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class BasePersenter implements BaseContract.IBasePersenter {
    private BaseContract.IBaseView mIBaseView;
    private BroadcastReceiver mWiredNetworkBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager mCm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mCm.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);

            if (mNetworkInfo != null && mNetworkInfo.isConnected()) {
                NetworkConnectedShow();
                MyClientApplication.setIsNetWork(true);
            } else {
                NetworkDisConnectedShow();
                MyClientApplication.setIsNetWork(false);
            }
        }
    };

    public BasePersenter(BaseContract.IBaseView tIBaseView) {
        mIBaseView = tIBaseView;
    }


    // 创建默认文件
    @Override
    public void createDekmediaFile(Context mContext) {
        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
        FileUtil.createDir(new File(AppConfig.DESKMEDIAFILE));
        String video_save_path = mSharedPreferences.getString(AppConfig.Video_SAVE_PATH, "");
        if (video_save_path.equals("")) {
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putString(AppConfig.Video_SAVE_PATH, AppConfig.DESKMEDIAFILE);
            editor.commit();
            checkSDCapacity(video_save_path, mContext);
        }
        //可立批标识文件
       // if (!FileUtil.isFileExist(AppConfig.DEFAULT_DESKMEDIA_IDENTITY_PATH)){
       //     FileUtil.createNewFile(AppConfig.DEFAULT_DESKMEDIA_IDENTITY_PATH);
       // }



    }

    public boolean checkSDCapacity(String video_save_path, Context mContext) {
        SDCardUtils mSDCardUtils = new SDCardUtils(mContext, video_save_path);
        long size = mSDCardUtils.getAvailableSize() / (1024 * 1024 * 1024);
        if (size < 1) {
            //MyToastUtils.ShortToaseShow(mContext, "该SD容量不足5G!");
            return false;
        }
        return true;
    }

    // 权限申请
    @Override
    public void requestPermissions(Context mContext) {
        new RxPermissions((Activity) mContext)
                .requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO)
                .subscribe(new io.reactivex.functions.Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {     // 用户已经同意该权限
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                        } else {   // 用户拒绝了该权限，并且选中『不再询问』
                        }
                    }
                });
    }

    @Override
    public void StartBroadcastReceiver(Context mContext) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        mContext.registerReceiver(mWiredNetworkBroadcastReceiver, intentFilter);
    }

    @Override
    public void StopBroadcastReceiver(Context mContext) {
        if (mWiredNetworkBroadcastReceiver != null) {
            mContext.unregisterReceiver(mWiredNetworkBroadcastReceiver);
        }
    }

    // 初始化数据库
    @Override
    public void initDB(Context context) {
        //初始化内存数据
        MemoryDaoUtils memoryDaoUtils = new MemoryDaoUtils(context);
        List<Memory> list = memoryDaoUtils.queryAllMemory();
        if (list.size() <= 0) {
            memoryDaoUtils.insertMemory(new Memory(null, AppConfig.MEMORY_TYPE_TOTAL_SIZE, AppConfig.TOTAL_SIZE));
            memoryDaoUtils.insertMemory(new Memory(null, AppConfig.MEMORY_TYPE_USED_SIZE, 0));
        }
        //初始化鼠标用户数据
        MouseUserDevicesinfoDaoUtils mouseUserDevicesinfoDaoUtils = new MouseUserDevicesinfoDaoUtils(context);
        List<MouseUserDevicesinfoBean> mouseList = mouseUserDevicesinfoDaoUtils.queryAll();
        if (mouseList == null || mouseList.size() == 0) {
            mouseUserDevicesinfoDaoUtils.clear();
            Date createTime = new Date();
            String creator = "ClickPostil";
            Date updateTime = new Date();
            String updator = "ClickPostil";
            int delFlag = 1;
            String deviceAttId = "1";
            boolean isShow=true;
            mouseUserDevicesinfoDaoUtils.clear();
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 1, deviceAttId, 1, context.getResources().getString(R.string.user_1), 5, "red_line", 0, "ff0000", AppConfig.STROKE_PEN,isShow));
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 2, deviceAttId, 2, context.getResources().getString(R.string.user_2), 5, "orange_line", 0, "ff9600", AppConfig.STROKE_PEN,isShow));
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 3, deviceAttId, 3, context.getResources().getString(R.string.user_3), 5, "orange_line", 0, "ffff00", AppConfig.STROKE_PEN,isShow));
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 4, deviceAttId, 4, context.getResources().getString(R.string.user_4), 5, "green_line", 0, "00ff00", AppConfig.STROKE_PEN,isShow));
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 5, deviceAttId, 5, context.getResources().getString(R.string.user_5), 5, "cyan_line", 0, "87ceeb", AppConfig.STROKE_PEN,isShow));
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 6, deviceAttId, 6, context.getResources().getString(R.string.user_6), 5, "blue_line", 0, "0000ff", AppConfig.STROKE_PEN,isShow));
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 7, deviceAttId, 7, context.getResources().getString(R.string.user_7), 5, "puple_line", 0, "ff00ff", AppConfig.STROKE_PEN,isShow));
            mouseUserDevicesinfoDaoUtils.insertMouseUserDevicesinfo(new MouseUserDevicesinfoBean(createTime, creator, updateTime, updator, delFlag, 8, deviceAttId, 8, context.getResources().getString(R.string.user_8), 5, "black_line", 0, "000000", AppConfig.STROKE_PEN,isShow));
        }



       /* VideoFileDaoUtils videoFileDaoUtils=new VideoFileDaoUtils(context);
        videoFileDaoUtils.deleteVideoFileAlll();
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议1-1",1531820766000L,"path",10000L,"会议1-1","1","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议1-2",1531820788000L,"path",10000L,"会议1-2","1","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议2-1",1519119966000L,"path",10000L,"会议2-1","2","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议2-2",1519119988000L,"path",10000L,"会议2-2","2","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议3-1",1521539199000L,"path",10000L,"会议3-1","2","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议3-2",1521539288000L,"path",10000L,"会议3-2","2","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议4-1",1524217566000L,"path",10000L,"会议4-1","3","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议4-2",1524217588000L,"path",10000L,"会议4-2","3","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议5-1",1523353566000L,"path",10000L,"会议5-1","4","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议5-2",1523353588000L,"path",10000L,"会议5-2","4","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议6-1",1522575966000L,"path",10000L,"会议6-1","5","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议6-2",1522575988000L,"path",10000L,"会议6-2","5","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议7-1",1526809566000L,"path",10000L,"会议7-1","6","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议7-2",1526809588000L,"path",10000L,"会议7-2","6","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议8-1",1525945566000L,"path",10000L,"会议8-1","7","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议8-2",1525945588000L,"path",10000L,"会议8-2","7","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议9-1",1525167966000L,"path",10000L,"会议9-1","8","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议9-2",1525167988000L,"path",10000L,"会议9-2","8","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议10-1",1529487966000L,"path",10000L,"会议10-1","9","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议10-2",1529487988000L,"path",10000L,"会议10-2","9","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议11-1",1528623966000L,"path",10000L,"会议11-1","10","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议11-2",1528623988000L,"path",10000L,"会议11-2","10","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议12-1",1527846366000L,"path",10000L,"会议12-1","11","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议12-2",1527846388000L,"path",10000L,"会议12-2","11","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议13-1",1530438366000L,"path",10000L,"会议13-1","12","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议13-2",1530438388000L,"path",10000L,"会议13-2","12","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议14-1",1513763166000L,"path",10000L,"会议14-1","13","视频会议1",1531820766000L));
        videoFileDaoUtils.insertVideoFile(new VideoFile(null,"会议14-2",1513763188000L,"path",10000L,"会议14-2","13","视频会议1",1531820766000L));
*/

    }

    // 网络断开展示
    @Override
    public void NetworkDisConnectedShow() {
        mIBaseView.wiredNetworkHidden();
    }

    // 网络连接展示
    public void NetworkConnectedShow() {
        mIBaseView.wiredNetworkShow();
    }


}
